<template>
    <div>
      <el-form :inline="true" :model="queryForm" class="demo-form-inline">
        <el-form-item label="论坛帖子管理">
          <el-input v-model="queryForm.kw" placeholder="标题" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
  
      <el-table v-loading="loading" :data="list">
        <!-- <el-table-column label="序号" type="index" width="100" /> -->
        <el-table-column align="center" prop="user.nickName" label="作者" width="160">
        </el-table-column>
        <el-table-column align="center" prop="content" label="被评论帖子" show-overflow-tooltip>
          <template #default="scope">
            <span v-if="scope.row.targetType=='NOTE'">[帖子]{{ scope.row.note.content }}</span>
            <span v-if="scope.row.targetType=='TRAVEL'">[京剧推荐]{{ scope.row.travelInfo.title }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="content" label="评论内容" show-overflow-tooltip>
        </el-table-column>
        <!-- <el-table-column align="center" prop="description" label="内容" show-overflow-tooltip>
        </el-table-column> -->
        <el-table-column label="回复时间" align="center" prop="createTime" width="130">
          <template #default="scope">
              <div>{{timestampToDayHM(scope.row.updatedAt)}}</div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="180">
          <template #default="scope">
            <el-button link type="danger" icon="Close" @click="handleDelete(scope.row.id, -1)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <el-pagination background layout="prev, pager, next" v-model:page-size="queryForm.pageSize" v-model:current-page="queryForm.pageNo" :total="total" @current-change="getList" />

      <!--详情对话框-->
    <el-dialog v-model="openDetail" title="帖子正文" width="60%" append-to-body v-if="detailData">
      <div class="introduction">{{ detailData.content }}</div>
    </el-dialog>
    </div>
  </template>
  <script setup lang="ts">
  import { ref, reactive, nextTick, toRef } from "vue";
  import baseService from "@/service/baseService";
  import { ElMessage, ElMessageBox } from "element-plus";
  import { timestampToDayHM, timestampToHM, timestampToDate, formatAmount } from "@/utils/utils"
  import { regionData, codeToText, TextToCode } from 'element-china-area-data'
  import { fileUploader } from '@/utils/upload.js'
  import { useRouter, useRoute } from 'vue-router';
  
  import { EluiChinaAreaDht } from 'elui-china-area-dht'
  const chinaData = new EluiChinaAreaDht.ChinaArea().chinaAreaflat

  const router = useRouter();
  const route = useRoute();
  // 查询参数
  const queryForm = reactive({
    kw: '',
    tag: 'FOOD',
    pageNo: 1,
    pageSize: 10
  });
  // 列表内容数量
  const total = ref(0);
  // 列表是否加载
  const loading = ref(true);
  // 列表返回值
  const list = ref<any[]>([]);
  const selectedCityOptions = ref<any[]>([]);
  
  // 表单实例
  const formRef = ref();
  // 是否打开弹出框
  const open = ref(false);
  const isEdit = ref(0);
  const openDetail = ref(false);
  // 弹出框标题
  const title = ref('');
  const detailData = ref();
  // 提交表单数据
  let editForm = toRef(
    reactive({
      id: '',
      title: '',
      tags: 'FOOD',
      mainPic: '',
      pics: '',
      videoUrl: '',
      description: '',
      favs: 0,
      likes: 0,
    })
  );
  const options = ref(regionData)
  const selectedOptions = ref<any>([])
  //多图上传的返回图片url数组
  const pics = ref<any>([])
  const uploadUrlPath = ref();

  // 表单验证
  const rules = ref({
    title: [{ required: true, message: "请输入", trigger: "blur" }],
    description: [{ required: true, message: "请输入", trigger: "blur" }],
  });
  
  /**
   * 查询列表
   */
  const getList = () => {
    loading.value = true;
    baseService
      .post("/admin/blog/comment/list", queryForm).then((res) => {
        loading.value = false;
        if (res.code === 200) {
          list.value = res.data.records;
          total.value = res.data.totalrecord;
        } else {
          list.value = [];
        }
      })
      .catch(() => {
        loading.value = false;
      });
  };
  
  /**
   * 搜索按钮操作
   */
  function handleQuery() {
    queryForm.pageNo = 1;
    getList();
  }
  
  //显示详情
function showDetail(data: any){
    openDetail.value = true;
    title.value = "活动正文";
    detailData.value = data
  }

  /**
   * 表单重置
   */
  function reset() {
    editForm.value = {
      id: '',
      title: '',
      tags: 'FOOD',
      mainPic: '',
      pics: '',
      videoUrl: '',
      description: '',
      favs: 0,
      likes: 0,
    };
    pics.value = []
  
    nextTick(() => {
      formRef.value?.resetFields();
    });
  }
  
  /**
   * 新增按钮操作
   */
  function handleAdd() {
    reset();
    open.value = true;
    isEdit.value = 0;
    console.log('图片:',pics.value)
    title.value = "添加";
  }
  
  /**
   * 删除按钮操作
   * @param id 主键
   */
  function handleDelete(id: any, optType: number) {
    let msg = '确认要删除该内容吗？'
    ElMessageBox.confirm(msg, "提示").then(() => {
      baseService.get(`/admin/blog/comment/delete/${id}`).then((res) => {
        if (res.code === 200) {
          ElMessage.success(res.msg);
          getList();
        } else {
          ElMessage.error(res.msg);
        }
      });
    });
  }

  getList();
  </script>
  
<style scoped>
.price{
color: #F56C6C;
font-weight: bold;
}
.pointer{
    cursor: pointer;
}
.pointer:HOVER{
    color: #409eff;
}
.introduction{
  width: 80%;
  margin: 15px auto;
  white-space: pre-line;
}
.addpic{
  font-size: 22px;
  padding: 20px 25px 15px 25px;
  border: 2px dashed #ccc;
  color: #888;
  height: 68px;
}
.img-list{
  display: flex;
  flex-wrap: wrap;
}
.img-list .img-li{
  width: 260px;
  position: relative;
  margin-right: 10px;
}
.logo-remove{
  font-size: 22px;
  position: absolute;
  top: 10px;
  right: 10px;
  color: #f00;
  cursor: pointer;
  text-shadow: #000 4px 4px 5px;
}
.logo-remove .el-icon{
  box-shadow: #fff 3px 5px 6px;
  background-color: #fff;
}
.logo-remove:HOVER{
  color: #F56C6C;
}
</style>
  